package org.chen.auth.service.impl;

import org.chen.auth.domain.Permission;
import org.chen.auth.service.IPermissionScanService;
import org.chen.auth.service.IPermissionService;
import org.chen.bas.enums.RequestTypeEnum;
import org.chen.bas.solution.Qing;
import org.chen.bas.util.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class IPermissionScanServiceImpl implements IPermissionScanService {
    @Autowired
    private IPermissionService service;

    @Value("${package-scan.url}")
    private String url;
    @Transactional
    @Override
    public void scan() {
        service.deleteAll();
        List<Class> classes = ClassUtils.getAllClassName(url);
        for (Class cla:classes){
            //判断字节码对象是否有权限注解
            Qing annotation = (Qing) cla.getAnnotation(Qing.class);
            if (annotation !=null){
                Permission permission1 = new Permission();
                permission1.setName(annotation.name());
                permission1.setDescs(annotation.desc());
                //sn:类名
                permission1.setSn(cla.getSimpleName());
                RequestMapping annotation2 = (RequestMapping) cla.getAnnotation(RequestMapping.class);
                //获取类上的url
                String clazzUrl = annotation2.value()[0];
                permission1.setUrl(clazzUrl);
                permission1.setParent(null);
                // 父类对象先入库
                service.insert(permission1);

                //获取方法名
                Method[] methods = cla.getMethods();
                System.out.println(Arrays.toString(methods));
                for (Method method:methods){
                    //判断方法上是否有注解
                    Qing annotation1 = method.getAnnotation(Qing.class);
                    if (annotation1 != null){
                        //拼接数据
                        Permission permission = new Permission();
                        permission.setDescs(annotation1.desc());
                        permission.setName(annotation1.name());
                        //sn 是类名+方法名
                        permission.setSn(cla.getSimpleName()+":"+method.getName());
                        String methodUrl = getMethodUrl(method);
                        permission.setUrl(clazzUrl+methodUrl);
                        permission.setParent(permission1);
                        service.insert(permission);
                    }
                }
            }
        }
    }
    /**
     * 获取方法上的 url
     * @param method
     * @return
     */
    public String getMethodUrl(Method method){
        String methodUrl = "";
        // 取枚举中定义的所有的请求类型
        RequestTypeEnum[] requestTypeEnums = RequestTypeEnum.values();
        for (RequestTypeEnum typeEnum : requestTypeEnums) {
            // 根据枚举中的不同的请求类型的class获取该类型对应的注解对象
            Annotation annotation = method.getAnnotation(typeEnum.getRequestType());
            // 如果该请求类型注解不存在就跳过
            if(Objects.isNull(annotation)){
                continue;
            }
            try {
                // 如果该请求类型注解存在,就获取它里面的value方法
                Class<? extends Annotation> aClass = annotation.annotationType();
                // aClass 是 对应的 请求类型的  字节码对象
                Method annotationMethod = aClass.getMethod("value");
                // 通过invoke调用该对象的value方法,获取结果
                // 反射执行 方法 方法名.invoke(对象)
                String[] value = (String[]) annotationMethod.invoke(annotation);
                // 如果value不为空且长度大于0,就赋值给methodUrl,并跳出循环
                if(value != null && value.length > 0){
                    methodUrl = value[0];
                    break;
                }
            }catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return methodUrl;

    }
}
